#include "Ctrl.h"

#if EventDelegation_

#include <functional>
#include <list>

typedef function<void()> Action;

class ObserverA//观察者类A
{
public:
	ObserverA() {}
	virtual ~ObserverA() {}
	virtual void ToStatusA()
	{
		cout << "执行动作A" << endl;
	}
};
class ObserverB//观察者类B
{
public:
	ObserverB() {}
	virtual ~ObserverB() {}
	virtual void ToStatusB()
	{
		cout << "执行动作B" << endl;
	}
};
class Subject//主题/通知者
{
private:
	list<Action*> acts;
public:
	Subject() {}
	virtual ~Subject() {}

	//注册事件
	void regist(Action* a)
	{
		acts.push_back(a);
	}

	//注销事件
	void unregist(Action* a)
	{
		acts.remove(a);
	}

	void Notify()
	{
		for (auto act : acts)
		{
			(*act)();
		}
	}
};


int main()
{
	ObserverA a;
	Action acA = bind(&ObserverA::ToStatusA, &a);

	ObserverB b;
	Action acB = bind(&ObserverB::ToStatusB, &b);

	Subject s;

	s.regist(&acA);
	s.regist(&acB);
	s.Notify();

	cout << "-----分割线-----" << endl;

	s.unregist(&acA);
	s.Notify();

	getchar();
	return 0;
}







#endif